package org.jground.rx.ui.zk.docu;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.dbfs.DBFile;
import org.jground.dbfs.IDBFile;
import org.jground.dbfs.IDBFileService;
import org.jground.rx.RXSystem;
import org.jground.rx.RXView;
import org.jground.rx.docu.DocuUtils;
import org.jground.rx.ui.IUIAttributeView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.viewControl.UIAttributeControlBase;
import org.jground.util.ArrayUtils;
import org.jground.util.IOUtils;
import org.jground.util.ImagingUtils;
import org.jground.util.StringUtils;
import org.jground.zk.zulX.BoxX;
import org.jground.zk.zulX.ZK;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Button;
import org.zkoss.zul.Image;
import org.zkoss.zul.impl.XulElement;

public class UIAttributeControlDocu extends UIAttributeControlBase implements
		EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static int MEDIUM_HEIGHT = 200;
	public static int SMALL_HEIGHT = 40;

	static Log logger = LogFactory.getLog(UIAttributeControlDocu.class);

	public IUIAttributeView<Component, UIFrame> createNew(UIFrame uiFrame,
			RXView moControl) {
		return new UIAttributeControlDocu((UIFrame) uiFrame, moControl);
	}

	public UIAttributeControlDocu() {
		super();
	}

	private Button uploadBt;
	private XulElement docuElement;
	private boolean isPicture;
	private BoxX box;
	private String filename;
	private UIFileDownload2 uiFileDownload;
	private UIImageView uiImageView;

	private UIAttributeControlDocu(UIFrame uiFrame, RXView rxControl) {
		super(uiFrame, rxControl);
		isPicture = UIDocuUtils.pictureUpload.equals(rxView.getWidgetHint());
		//
		if (isPicture) {
			uploadBt = new Button("Upload Picture (JPG)");
		} else {
			uploadBt = new Button("Upload File");
		}
		uploadBt.setUpload("true");
		uploadBt.addEventListener(Events.ON_CLICK, this);
		uploadBt.addEventListener(Events.ON_UPLOAD, this);
		uiFileDownload = new UIFileDownload2();
		uiImageView = new UIImageView();
		//
		box = new BoxX();
		box.setOrient(ZK.AttValue.horizontal);
		box.setAlign(ZK.AttValue.bottom);
		box.setSpacing("8px");
		add(box);
		//
	}

	public void set(String... values) {
		clear();
		if (ArrayUtils.isNotEmpty(values) && StringUtils.isNotEmpty(values[0])) {
			filename = values[0];
			long oid = getDataContext().getOid();
			IDBFile file = DocuUtils.getFile(oid, filename);
			uiFileDownload.setFile(file);
			if (isPicture && ImagingUtils.isExtensionSupported(filename)) {
				Image icon = new Image();
				icon.setContent(UIDocuUtils.getImage(oid,
						UIAttributeControlDocu.SMALL_HEIGHT, filename));
				docuElement = icon;
				//
				uiImageView.setAImageFile(file);
				//
				// box.set(docuElement, uiFileDownload, uiImageView);
				box.set(uiFileDownload, docuElement);
			} else {
				// docuElement = new Label(filename);
				// box.set(docuElement, uiFileDownload);
				box.set(uiFileDownload);
			}
		}
	}

	public String[] get() {
		return StringUtils.isEmpty(filename) ? new String[] {}
				: new String[] { filename };
	}

	public void clear() {
		filename = null;
		box.set(uploadBt);
	}

	public void onEvent(Event he) {

		if (he.getTarget() == uploadBt) {
			if (he instanceof UploadEvent) {
				UploadEvent ue = (UploadEvent) he;
				IDBFileService fs = RXSystem.getInstance().getFileService();
				try {
					Media media = ue.getMedia();
					if (media == null) {
						return;
					}
					filename = media.getName();
					long oid = getDataContext().getOid();
					// TODO : check file name

					IDBFile file = DocuUtils.newFile(oid, filename);
					byte[] data = null;
					if (media.isBinary()) {
						data = IOUtils.toByteArray(media.getStreamData());
					} else {
						String string = media.getStringData();
						data = string.getBytes(DBFile.encoding);
					}

					fs.save(file, data);
					if (isPicture
							&& ImagingUtils.isExtensionSupported(filename)) {
						//
						//
						//
						IDBFile mFile = DocuUtils.newImageFile(oid,
								MEDIUM_HEIGHT, filename);
						byte[] mediumData = ImagingUtils.resizeJPGToHeight(
								data, MEDIUM_HEIGHT);
						fs.save(mFile, mediumData);
						//
						//
						//
						IDBFile sFile = DocuUtils.newImageFile(oid,
								SMALL_HEIGHT, filename);
						byte[] smallData = ImagingUtils.resizeJPGToHeight(data,
								SMALL_HEIGHT);
						fs.save(sFile, smallData);
					}
					set(ArrayUtils.toArray(filename));
				} catch (Exception e) {
					logger.error(e, e);
				} finally {

				}

			}
		}
	}
}